import { Avatar, Button, List, message } from "antd";
import { FC } from "react";
import { httpPost } from "@utils/HttpUtil";
import ManageProblemKnowledge from "./manageproblem/ManageProblemKnowledge";
import ManageProblemPreview from "./manageproblem/ManageProblemPreview";

const deleteProblem = (problemId:any)=>{
  var status = httpPost("/problem/deleteProblem", {
    problemId: problemId
  }).data;
  if(status){
    message.success("删除成功");
  }else{
    message.error("删除失败");
  }
}

const FunctionManageProblem = () => {
    var data = httpPost("/problem/getProblemList", {}).data;
    return (
      <>
        <List
          itemLayout="vertical"
          pagination={{
            onChange: (page) => {
              console.log(page);
            },
            pageSize: 3,
          }}
          dataSource={data}
          renderItem={(item: any) => {
            var knowledgeList = httpPost("/problemKnowledge/getProblemKnowledgeList", {
              problemId: item.content.id,
            }).data;
            return (
              <List.Item
                style={{ margin: 20, width: 600 }}
                key={item.id}
                actions={[
                  <ManageProblemKnowledge
                    knowledgeList={knowledgeList}
                    problemId={item.content.id}
                  />,
                  <Button
                    type="link"
                    onClick={() => {
                      deleteProblem(item.content.id);
                    }}
                  >
                    删除题目
                  </Button>,
                ]}
              >
                <List.Item.Meta
                  avatar={<Avatar src={item.avatar} onClick={() => {}} />}
                  description={<a>{"上传用户：" + item.description}</a>}
                />
                <ManageProblemPreview knowledgeList={knowledgeList} />
              </List.Item>
            );
          }}
        />
      </>
    );
};

export default FunctionManageProblem;